<?php

/**
 * This is the model class for table "demande".
 *
 * The followings are the available columns in table 'demande':
 * @property integer $id
 * @property integer $idseliste
 * @property integer $idgrain
 * @property integer $nb
 * @property string $date_souhait
 * @property string $modecontact
 * @property string $status
 * @property string $date_demande
 * @property string $commentaire
 *
 * The followings are the available model relations:
 * @property Grain $idgrain0
 * @property Seliste $idseliste0
 * @property Offre[] $offres
 */
class Demande extends CActiveRecord
{
	
	public $type;
	public $domaine;
	
	
	public function init() {
		$this->type = new Type;
		$this->domaine = new Domaine();
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'demande';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idseliste, idgrain, modecontact', 'required'),
			array('idseliste, idgrain, nb', 'numerical', 'integerOnly'=>true),
			array('modecontact, status', 'length', 'max'=>1),
			array('date_souhait, date_demande, commentaire', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, idseliste, idgrain, nb, date_souhait, modecontact, status, date_demande, commentaire', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idgrain0' => array(self::BELONGS_TO, 'Grain', 'idgrain'),
			'idgrain1' => array(self::BELONGS_TO, 'Grain', 'idgrain'),
			'idseliste0' => array(self::BELONGS_TO, 'Seliste', 'idseliste'),
			'selistes' => array(self::MANY_MANY, 'Seliste', 'demande_seliste(iddemande, idseliste)'),
			'offres' => array(self::HAS_MANY, 'Offre', 'iddemande'),
			'type0'=>array(self::HAS_ONE,'Type',array('idtype'=>'id'),'through'=>'idgrain1'),
			'domaine0'=>array(self::HAS_ONE,'Domaine',array('iddomaine'=>'id'),'through'=>'idgrain0'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Numéro',
			'idseliste' => 'Demandeur',
			'idgrain' => 'Grain',
			'nb' => 'Nombre d\'unités',
			'date_souhait' => 'Date souhaitée',
			'modecontact' => 'Mode de contact préféré',
			'status' => 'Status',
			'date_demande' => 'Date demande',
			'commentaire' => 'Commentaire',
			'date_stricte' => 'Impérativement le jour souhaité',
			'date_expiration' => 'Date d\'expiration'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		if ($this->type->id>0) $criteria->compare('idgrain0.idtype',$this->type->id,true);
		if ($this->domaine->id>0) $criteria->compare('idgrain0.iddomaine',$this->domaine->id,true);
		if ($this->idseliste>0) $criteria->compare('t.idseliste',$this->idseliste,true);
		/*$criteria->compare('idgrain',$this->idgrain);
		$criteria->compare('nb',$this->nb);
		$criteria->compare('date_souhait',$this->date_souhait,true);
		$criteria->compare('modecontact',$this->modecontact,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('date_demande',$this->date_demande,true);
		$criteria->compare('commentaire',$this->commentaire,true);*/
		$criteria->with = array('idseliste0','idgrain0','type0','domaine0');
		
		$sort = new CSort();
		$sort->attributes = array(
				'id',
				'sgrain'=>array(
						'asc'=>'idgrain0.nom asc',
						'desc'=>'idgrain0.nom desc',
						'label'=>'Grain'
				),
				'stype'=>array(
						'asc'=>'type0.lib asc',
						'desc'=>'type0.lib desc',
						'label'=>'Type'
				),
				'sdomaine'=>array(
						'asc'=>'domaine0.lib asc',
						'desc'=>'domaine0.lib desc',
						'label'=>'Domaine'
				),
				'seliste'=>array(
						'asc'=>'idseliste0.pseudo asc',
						'desc'=>'idseliste0.pseudo desc',
						'label'=>'Séliste'
				),
				'date_souhait',
				'date_stricte',
				'date_expiration'
		);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>$sort,
			'pagination'=>array(
					'pageSize'=>40,
			),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Demande the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getNbDemandes() {
		return Demande::model()->countByAttributes(array(
            'idseliste'=> Yii::app()->user->id,
			'status'=>1,
        ));
	}
}
